/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestInt.controlador;

import gestInt.DAO.AdministrativoJpaController;
import gestInt.DAO.exceptions.NonexistentEntityException;
import gestInt.DAO.exceptions.RollbackFailureException;
import gestInt.modelo.Administrativo;
import gestInt.modelo.AdministrativoPK;
import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;
import org.primefaces.context.RequestContext;

/**
 *
 * @author elkin
 */
@Named(value = "administrativoController")
@SessionScoped
public class AdministrativoController implements Serializable {

    @Resource
    private UserTransaction utx;

    private Administrativo objAdministrativo;
    private final AdministrativoJpaController jpaAdministrativo;
    private List<Administrativo> lstAdministrativos;
    boolean encontrado;

    /**
     * Creates a new instance of AdministrativoController
     */
    public AdministrativoController() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("GestIntPU");
        this.objAdministrativo = new Administrativo();
        this.jpaAdministrativo = new AdministrativoJpaController(utx, emf);
        this.encontrado = false;
        this.lstAdministrativos = this.jpaAdministrativo.findAdministrativoEntities();
    }

    public void registrarAdministrativo() {
        
        for (Administrativo administrativo : lstAdministrativos) {
            int codigo1 = this.objAdministrativo.getAdministrativoPK().getIdAdministrativo();
            int codigo2 = administrativo.getAdministrativoPK().getIdAdministrativo();
            if (codigo1 == codigo2) {
                this.encontrado = true;
            }
        }

        if (!this.encontrado) {
            try {
                this.jpaAdministrativo.create(objAdministrativo);
            } catch (RollbackFailureException ex) {
                Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Registro exitoso", null));

        } else {
            this.encontrado = false;
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "El codigo suministrado ya ha sido registrado", null));
        }

    }

    public void eliminarAdministrativo() {
        AdministrativoPK idEliminar = this.objAdministrativo.getAdministrativoPK();
        try {
            this.jpaAdministrativo.destroy(idEliminar);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("");
        } catch (IOException ex) {
            Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void editarAdministrativo() {
        try {
            this.jpaAdministrativo.edit(this.objAdministrativo);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(AdministrativoController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void limpiar() {
        System.out.println("en limpiar");
        RequestContext.getCurrentInstance().reset("form:panel");
    }

    public void limpiarObjAdministrativo() {

    }

    public List<Administrativo> getLstAdministrativos() {
        return lstAdministrativos;
    }

    public void setLstAdministrativos(List<Administrativo> lstAdministrativos) {
        this.lstAdministrativos = lstAdministrativos;
    }

    public Administrativo getObjAdministrativo() {
        return objAdministrativo;
    }

    public void setObjAdministrativo(Administrativo objAdministrativo) {
        this.objAdministrativo = objAdministrativo;
    }

}
